package com.dhcc.bpm.common.utils;

import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;

/**
 * @Title
 * @ClassName WorldUtils
 * @Desription
 * @Author zhangzexu
 * @Date 2020/6/1 09:07
 * @Version V1.0
 */
public class WorldUtils {
    /**
     * HTML转word
     *
     * @param noteName 导出文件名称
     * @paramre portDirName 文件路径
     * @param researchNoteInfo 文件的html
     * @return void
     * @author Solitary
     * @date 2019/1/11 9:21
     */
    public static void htmlToWord(String noteName, String reportDirName, String researchNoteInfo) throws Exception {
        //拼一个标准的HTML格式文档
        Document document = Jsoup.parse(researchNoteInfo);
        File file = new File(reportDirName);
        if(!file.exists()){
            file.mkdirs();
        }
        InputStream is = new ByteArrayInputStream(document.html().getBytes("UTF-8"));
        OutputStream os = new FileOutputStream(reportDirName+noteName);
        inputStreamToWord(is, os);
    }

    /**
     * 把is写入到对应的word输出流os中
     *
     * @param is
     * @param os
     * @throws IOException
     */
    private static void inputStreamToWord(InputStream is, OutputStream os) throws IOException {
        POIFSFileSystem fs = new POIFSFileSystem();
        DirectoryNode root = fs.getRoot();
        root.createDocument("WordDocument", is);
        fs.writeFilesystem(os);
        os.close();
        is.close();
    }

}
